package com.auto.learning.view;

import java.util.List;


import com.auto.learning.R;
import com.auto.learning.adapter.CommonAdapter;
import com.auto.learning.adapter.ViewHolder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

public class Pup_Zb_Dialog {
	private PopupWindow popupWindow;
	private EditText et_message;
	private GridView mGridview;
	private CommonAdapter<String> adapter;
	private int defaultCheckMoney = 0;
	private List<String> list;
	private Context context;

	public Pup_Zb_Dialog(final Context context, List<String> list) {
		this.list = list;
		this.context = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.pup_zhubodialog, null);
		et_message = (EditText) view.findViewById(R.id.et_message);
		mGridview = (GridView) view.findViewById(R.id.gr_money);
		adapter = new CommonAdapter<String>(context, list,
				R.layout.item_zb_money) {
			@SuppressLint("NewApi")
			@Override
			public void convert(ViewHolder viewHolder, String item) {
				// TODO Auto-generated method stub
				RelativeLayout rl_bg = viewHolder.getView(R.id.rl_bg);
				if (viewHolder.getPosition() == defaultCheckMoney) {
					rl_bg.setBackground(context.getResources().getDrawable(
							R.drawable.item_zb_money_bg));
				} else {
					rl_bg.setBackground(context.getResources().getDrawable(
							R.drawable.item_zb_money_bg1));
				}
				viewHolder.setText(R.id.tv_text, item);
			}
		};
		mGridview.setAdapter(adapter);
		mGridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				defaultCheckMoney = position;
				adapter.notifyDataSetChanged();
			}
		});
		view.findViewById(R.id.im_close).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dismiss();
					}
				});
		popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT, true);
		popupWindow.setTouchable(true);
		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				Log.i("mengdd", "onTouch : ");

				return false;
				// 这里如果返回true的话，touch事件将被拦截
				// 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
			}
		});
		// 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
		// 我觉得这里是API的一个bug
		popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
				R.drawable.pupbg));

	}

	public String getMessageText() {
		return et_message.getText().toString();
	}

	public String getMoney() {
		return list.get(defaultCheckMoney);
	}

	public void show(View view) {
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);
		int height = metrics.heightPixels;
		Rect frame = new Rect();
		((Activity) context).getWindow().getDecorView()
				.getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		popupWindow.showAtLocation(view, Gravity.CENTER, 0, statusBarHeight);
	}

	public void dismiss() {
		popupWindow.dismiss();
	}
}
